package com.chinasoft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinasoft.domain.Employee;
import com.chinasoft.service.EmployeeService;
import com.chinasoft.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController// = @Controller + @ResponseBody
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 新增和修改
     * @param
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpDate(@RequestBody Employee employee){

        try {
            employeeService.saveOrUpdate(employee);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("程序错误，请联系管理员");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("程序错误，请联系管理员");
        }
    }

    /**
     * 查询单条记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.getById(id);
    }
    /**
     * 查询所有
     */
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.list();
    }
    /**
     * 分页查询
     */
    @PostMapping
    public Page<Employee> queryPage(HttpServletRequest request){
        Page<Employee> employeePage = new Page<>(0,10);
        Page<Employee> page = employeeService.page(employeePage);
        return page;
    }
}
